﻿using System;
using System.Data;

namespace TreeViewBindingTest
{
    // populate on demand
    // null or zero as the parent
    // display order
    // test for problems
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DataSet dataSet = new DataSet();

                dataSet.Tables.Add("Table");
                dataSet.Tables[0].Columns.Add("ID", typeof(string));
                dataSet.Tables[0].Columns.Add("ParentID", typeof(string));
                dataSet.Tables[0].Columns.Add("Text", typeof(string));

                DataRow row = dataSet.Tables[0].NewRow();
                row["ID"] = "1";
                row["Text"] = "Parent 1";
                dataSet.Tables[0].Rows.Add(row);

                row = dataSet.Tables[0].NewRow();
                row["ID"] = "2";
                row["Text"] = "Parent 2";
                dataSet.Tables[0].Rows.Add(row);

                row = dataSet.Tables[0].NewRow();
                row["ID"] = "3";
                row["ParentID"] = "1";
                row["Text"] = "Child 1";
                dataSet.Tables[0].Rows.Add(row);

                row = dataSet.Tables[0].NewRow();
                row["ID"] = "4";
                row["ParentID"] = "1";
                row["Text"] = "Child 2";
                dataSet.Tables[0].Rows.Add(row);

                row = dataSet.Tables[0].NewRow();
                row["ID"] = "5";
                row["ParentID"] = "2";
                row["Text"] = "Child 3";
                dataSet.Tables[0].Rows.Add(row);

                // You can use this:
                TreeView1.DataSource = new HierarchicalDataSet(dataSet, "ID", "ParentID");

                // OR you can use the extensions for TreeView if you are using .NET 3.5
                //TreeView1.SetDataSourceFromDataSet(dataSet, "ID", "ParentID");
                
                // OR This line, will load the tree starting from the parent record of value = 1
                //TreeView1.DataSource = new HierarchicalDataSet(dataSet, "ID", "ParentID", 1);

                TreeView1.DataBind();
            }
        }
    }
}


